/**----------------------------------------------------------*/
/** JS:  cmi_categroy_list.js（商品管理--商品类型）           */
/**----------------------------------------------------------*/

var tipc = 'GOODS_CATEGROY';
var win = null;

var add = function() {
	try {
		var div = getEditDiv(null);
		win = MyUtils._getToolbarWin("dataPanel", "dateEdit", div, submitAdd, panel,"保存提交");
		win.window({
			width:450,
			height:270,
			modal: true
		});
	} catch (e) {
		showJsException(e);
	}
};

var modify = function() {
	try{
		var record = datagridOneRow('dataPanel');
		if(record == null) {
			return;
		}
		var div = getEditDiv(record);
		win = MyUtils._getToolbarWin("dataPanel", "dateEdit", div, submitModify, panel,"保存提交");
		win.window({
			width:450,
			height:270,
			modal: true
		});
	} catch(e) {
		showJsException(e);
	}
};

var remove = function() {
	try{
		var rows = $('#dataPanel').datagrid("getSelections");
		if(rows.length == 0) {
			MyUtils.waring(_empty_select);
			return;
		}
		var ids = new Array();
		var names = new Array();
		for(var i = 0; i < rows.length; i++) {
			ids.push(rows[i].uuid);
			names.push(rows[i].categroy);
		}
//		var data = { data:"["+MyUtils.createQueryConditionBean("uuid","customer","uuid IN (" + ids.toString() + ")","0",null) +"]"
		var data = { data:ids.toString()
			    ,st:tipc
			    ,msg:names.toString()
		};
		var remove_msg = '删除商品类型将会清除该类型下的所有属性。您确定要删除选定的商品类型吗？';
		$.messager.confirm(_msg_sys,MyUtils.valueAlign(remove_msg,"left"), function(r) {
			if (r) {
				MyUtils.openMask(_msg_process,$('#t2'));
				MyUtils.sendPost("impl_remove/backRemove.adm",data,function(rets){
					try{
						MyUtils.hiddenMask($('#t2'));
						var res = MyUtils.toJsonObject(rets);
						if(res.error == '0') {
							MyUtils.info(res.msg);
							$('#dataPanel').datagrid("clearSelections");
							$('#dataPanel').datagrid("reload");
						} else {
							MyUtils.error(res.msg);
						}
					} catch (e) {
						showJsException(e);
					}
				});
			}
		});
		
	} catch(e) {
		showJsException(e);
	}
};

var filedSet = function() {
	try{
		var record = datagridOneRow('dataPanel');
		if(record == null) {
			return;
		}
		parent.$('#tabs').tabs('close',"商品属性");
		parent.addTab("商品属性","innerJump.adm?st=GOODS_CATEGROY_PROPERTIES&spt="+record.uuid,"icon-sys");
		parent.$('#tabs').tabs('close',panel);
	} catch(e) {
		showJsException(e);
	}
};

var dbclick = function(index,row){
	$('#dataPanel').datagrid("clearSelections");
	$('#dataPanel').datagrid("selectRow",index);
	modify();
};

var initPanel = function() {
	var columns = [  {field : 'categroy',title : '商品类型名称',width : 150,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'groups',title : '属性分组',width : 250,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"left");}}
					,{field : 'fieldCt',title : '属性数',width : 250,align : 'center',formatter:function(value){return MyUtils.valueAlign(value,"right");}}
					,{field : 'state',title : '状态',width : 100,align : 'center',formatter:function(value){return '<img alt="OK" src="'+okPic+'" style="border: 0px;">';}}
				];
	var pam = {qr:"[]",st:tipc,sco:'',dc:Dmlx.sort.desc};
	MyUtils.createPaginPanelWithQueryparams('dataPanel',null,Dmlx._hFrozenColumns,columns,toolbars,dbclick, panel,null);
	$('#dataPanel').datagrid({
		url:"impl_search/commonsPaginSearch.ajax",
		queryParams:pam,
		pagination:true,
		fitColumns:true
	});
};

$(function(){
	try {
		initPanel();
	} catch (e) {
		showJsException(e,"数据加载失败!");
	}	
});

var submitAdd = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			var data = {dt:dt,st:tipc};
			MyUtils.openMask(_msg_process,win);
			submitDate("impl_add/backAdd.adm",data);
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var submitModify = function(){
	try {
		var dt = editValidator();
		if(dt != null) {
			var data = {dt:dt,st:tipc,qy:"[]"};
			$.messager.confirm(_msg_sys,MyUtils.valueAlign(_modify_confirm,"left"), function(r) {
				if(r) {
					MyUtils.openMask(_msg_process,win);
					submitDate("impl_modify/backModify.adm",data);
				}
			});
		}
	} catch (e) {
		MyUtils.hiddenMask(win);
		showJsException(e,_msg_fail);
	}
};

var editValidator = function(){
	var record = {
			 uuid:$('#uuid').val()
			,categroy:$('#categroy').val()
			,groups:$('#groups').val()
		};
	
	//验证用户账号
	if(MyUtils.isEmpty(record.categroy)) {
		MyUtils.waring("商品类型名称不能为空");
		return null;
	}
	return MyUtils.toJsonStr(record);
};

var submitDate = function(url,data) {
	MyUtils.sendPost(url,data,function(msg){
		try{
			MyUtils.hiddenMask(win);
			var res = MyUtils.toJsonObject(msg);
			if(res.error == '0') {
				if(win)win.window('close');
				MyUtils.info(res.msg);
				$('#dataPanel').datagrid("clearSelections");
				$('#dataPanel').datagrid("reload");
			} else {
				MyUtils.error(res.msg);
			}
		} catch (e) {
			MyUtils.hiddenMask(win);
			MyUtils.error(_msg_fail);
		}
	});
};

var getEditDiv = function(record) {
	var div= '<table cellspacing="2" cellpadding="6" id="dateEdit">'
			+'<tbody>'
				+'<tr>'
					+'<th align="right">商品类型名称</td>'
					+'<td align="left">'
					+'<input type="text" class="dateEdit" maxlength="120" id="categroy" style="width: 288px;" value="'+(record ? record.categroy : "")+'"><label class="requre_field">*</label>'
					+'<input type="hidden"  id="uuid" value="'+(record ? record.uuid : "")+'">'
					+'</td>'
				+'</tr>'
				+'<tr>'
					+'<th align="right">属性分组</td>'
					+'<td align="left"><textarea rows="6" cols="31" id="groups" name="groups" style="width: 300px;">'+(record ? record.groups: "")+'</textarea>'
					+'<div style="color:gray;">每行一个商品属性组。排序也将按照自然顺序排序。</div>'
					+'</td>'
				+'</tr>'
			+'</tbody>'
		+'</table>';
	return div;
};

